//
//  MyWidgetContentViewModel.swift
//  TimMusic
//
//  Created by nkl on 2024/10/24.
//

import Foundation
import KLExtension
import SwiftUI

class MyWidgetContentViewModel: ObservableObject {
    @Published var dataModel: MyWidgetsModel = .init(datas: [])
    @Published var isEditing: Bool = false

    var smallDatas: [MyWidgetItemModel] {
        dataModel.datas.filter {
            $0.itemModel.type == .small
        }
    }

    var mediaDatas: [MyWidgetItemModel] {
        dataModel.datas.filter {
            $0.itemModel.type == .medium
        }
    }

    func requestData() {
        if let data = TimDataManager.shared.fetchMyWidgets() {
            dataModel = data
        } else {
            dataModel = .init(datas: [])
        }
    }

    func deleteMyWidgets(id: String) {
        TimDataManager.shared.deleteMyWidget(id: id) { _ in
            requestData()
        }
    }
}
